﻿using System;

using NUnit.Framework;

namespace Rsdn.SmartApp.ActiveParts
{
	[TestFixture]
	public class ActivePartsTest
	{
		private ServiceManager _svcMgr;
		private ExtensionManager _extMgr;

		[SetUp]
		protected void SetUp()
		{
			_svcMgr = new ServiceManager();
			_extMgr = new ExtensionManager(_svcMgr);
			_extMgr.Scan(new[] { typeof(TestActivePart) }, new ActivePartRegStrategy(_svcMgr));
			_svcMgr.Publish<IActivePartManager>(new ActivePartManager(_svcMgr));
		}

		[Test]
		public void ElementNotRegistered()
		{
			Assert.IsFalse(_svcMgr.GetService<IActivePartManager>().IsPartRegistered("XXX"));
		}

		[Test]
		[ExpectedException(typeof(ArgumentException))]
		public void ElementNotRegisteredException()
		{
			_svcMgr.GetService<IActivePartManager>().GetPartInstance("XXX");
		}

		[Test]
		public void RegistrationTest()
		{
			Assert.AreEqual(typeof(TestActivePart),
				_svcMgr.GetService<IRegKeyedElementsService<string, ActivePartInfo>>()
					.GetElement(TestActivePart.Name).Type);
		}

		[Test]
		public void InstanceCreationTest()
		{
			Assert.IsNotNull(_svcMgr.GetService<IActivePartManager>().GetPartInstance(TestActivePart.Name));
		}

		[Test]
		public void InstanceCaching()
		{
			var partMgr = _svcMgr.GetService<IActivePartManager>();
			var inst1 = partMgr.GetPartInstance(TestActivePart.Name);
			var inst2 = partMgr.GetPartInstance(TestActivePart.Name);
			Assert.AreEqual(inst2, inst1);
		}

		[Test]
		public void GetPartTypeTest()
		{
			Assert.AreEqual(
				typeof (TestActivePart),
				_svcMgr.GetService<IActivePartManager>().GetPartType(TestActivePart.Name));
		}
	}
}
